package com.gensdai.leliang.common.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.gensdai.leliang.R;
import com.gensdai.leliang.entity.Address;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.annotations.NonNull;

/**
 * Created by zsxx on 2017/6/21.
 */

public class AddressAdapterRec extends DelegateAdapter.Adapter<AddressAdapterRec.MainViewHolder> {


    private Context mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;

    List<Address> blist;

    int oldDefaultPosition;

    OnAddressMenuClick menuClick;

    public AddressAdapterRec(Context context, LayoutHelper layoutHelper, List<Address> list) {
        this(context, layoutHelper, list, new VirtualLayoutManager.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    public AddressAdapterRec(Context context, LayoutHelper layoutHelper, List<Address> list, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.blist = list;
        this.mLayoutParams = layoutParams;
    }

    public void setOnAddressMenuClick(OnAddressMenuClick menuClick){
        this.menuClick = menuClick;
    }


    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.delivery_address_list_rec, parent, false));
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, final int position) {
        holder.itemView.setLayoutParams(
                new VirtualLayoutManager.LayoutParams(mLayoutParams));

        final Address address = blist.get(position);


        if (address.getDefault().equals("1")) {
            oldDefaultPosition = position;
            Drawable img = ContextCompat.getDrawable(mContext, R.mipmap.mrdz);
            img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
            holder.addressDefault.setCompoundDrawables(img, null, null, null);
            holder.addressDefault.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimaryDark));
        } else {
            holder.addressDefault.setTextColor(ContextCompat.getColor(mContext, R.color.liuge6));
            Drawable img = ContextCompat.getDrawable(mContext, R.mipmap.yuan);
            img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
            holder.addressDefault.setCompoundDrawables(img, null, null, null);
        }

        holder.name.setText(address.getUser_name());
        holder.phone.setText(address.getPhone());
        holder.address.setText(address.getCityId() + " " + address.getAddress());


        //编辑按钮的实现
        holder.editor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menuClick != null) {
                    menuClick.OnEditClick(address, position);
                }
            }
        });

        //删除按钮的实现
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menuClick != null) {
                    menuClick.OnDeleteClick(address, position);
                }
            }
        });

        //默认按钮的实现
        holder.addressDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menuClick != null) {
                    menuClick.OnsetDefaultAddressClick(address,oldDefaultPosition, position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return blist.size();
    }


    static class MainViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.phone)
        TextView phone;
        @BindView(R.id.address)
        TextView address;
        @BindView(R.id.address_default)
        TextView addressDefault;
        @BindView(R.id.editor)
        TextView editor;
        @BindView(R.id.delete)
        TextView delete;


        public MainViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }


    public interface OnAddressMenuClick {
        void OnsetDefaultAddressClick(Address address, int oldposition,int position);

        void OnDeleteClick(Address address, int position);

        void OnEditClick(Address address, int position);
    }
}
